package com.example.shiwuyouproject.ui

import android.content.Intent
import android.os.Bundle
import android.util.Log
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.wechat.friends.Wechat
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityLoginBinding
import com.example.shiwuyouproject.net.bean.User
import com.example.shiwuyouproject.net.http.BaseUserBean
import com.example.shiwuyouproject.net.http.QiNiuUpImageUtils
import com.example.shiwuyouproject.ui.fragment.my.activity.ChangePasswordActivity
import com.example.shiwuyouproject.ui.modelview.LoginModelView
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.example.shiwuyouproject.utils.ActStartUtils
import com.example.shiwuyouproject.utils.SPUtils
import com.example.shiwuyouproject.utils.ShareUtils
import com.example.shiwuyouproject.utils.SpConstant
import com.yechaoa.yutilskt.ShareUtil
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils
import java.util.HashMap

/**
 * ls
 * 登录
 */
class LoginActivity : BaseVmActivity<ActivityLoginBinding, LoginModelView>() {
    /**
     * 获取ViewModel的class
     */
    override fun viewModelClass(): Class<LoginModelView> = LoginModelView::class.java

    override fun getViewBinding(): ActivityLoginBinding {
        return ActivityLoginBinding.inflate(layoutInflater)
    }

    override fun initData() {
        if (!SPUtils.getInstance().getString(SpConstant.USER_PHONE).isNullOrEmpty() && !SPUtils.getInstance().getString(SpConstant.USER_PWD).isNullOrEmpty()) {
            mViewModel.userLogin(
                    SPUtils.getInstance().getString(SpConstant.USER_PHONE),
                    SPUtils.getInstance().getString(SpConstant.USER_PWD)
            )
        }
    }

    private var bind_open_id = ""
    private var bind_uniq_id = ""
    override fun setListener() {
        mBinding.btLogin.setOnClickListener {
            if (mBinding.edPhoneNumber.text.length != 11) {
                ToastUtil.show("您输入的手机号位数不对！")
                return@setOnClickListener
            }
            if (mBinding.edPwd.text.isEmpty()) {
                ToastUtil.show("您输入密码！")
                return@setOnClickListener
            }
            YUtils.showLoading(this, "登录中...")
            mViewModel.userLogin(
                    mBinding.edPhoneNumber.text.toString(),
                    mBinding.edPwd.text.toString()
            )
        }
        mBinding.btRegister.setOnClickListener {
            ActStartUtils.startAct(this, RegisterActivity::class.java)
        }
        mBinding.tvExit.setOnClickListener { finish() }
        mBinding.wangjimima.setOnClickListener { //忘记密码
            val mBundle = Bundle()
            mBundle.putString("stTitle", "忘记密码")
            mBundle.putString("phone", mBinding.edPhoneNumber.text.toString())
            ActStartUtils.startForAct(this, ChangePasswordActivity::class.java, mBundle, 100)
        }
        mBinding.btWeixing.setOnClickListener { //微信登录
            ShareUtils.onLogin(Wechat.NAME, object : PlatformActionListener {
                override fun onComplete(p0: Platform?, p1: Int, p2: HashMap<String, Any>?) {
//                    YUtils.showLoading(this@LoginActivity, "登录中...")
                    bind_open_id = p0?.db?.get("openid").toString()
                    bind_uniq_id = p0?.db?.get("unionid").toString()
                    mViewModel.getUserWxLogin(bind_open_id, bind_uniq_id)
                }

                override fun onError(p0: Platform?, p1: Int, p2: Throwable?) {
                    ToastUtil.show("微信登录错误！")
                }

                override fun onCancel(p0: Platform?, p1: Int) {
                    ToastUtil.show("微信登录取消！")
                }
            })
        }
    }

    override fun observe() {
        mViewModel.userLoginState.observe(this,androidx.lifecycle.Observer<Boolean> {
            YUtils.hideLoading()
            ToastUtil.show("登录成功！")
            ActStartUtils.startAct(this@LoginActivity, MainActivity::class.java)
            finish()
        })
        mViewModel.getUserWxLoginStatus.observe(this, androidx.lifecycle.Observer<BaseUserBean<User>>{
            if (it.step == "2") {
                YUtils.hideLoading()
                ToastUtil.show("登录成功！")
                finish()
            } else {
                val mBundle = Bundle()
                mBundle.putString("bind_open_id", bind_open_id)
                mBundle.putString("bind_uniq_id", bind_uniq_id)
                ActStartUtils.startForAct(this, AccountBindingActivity::class.java, mBundle, 1000)
                ToastUtil.show("您还未绑定微信，先绑定后登录！！")
            }
        })
        mViewModel.messageStatus.observe(this,androidx.lifecycle.Observer<String> {
            YUtils.hideLoading()
            ToastUtil.show(it)
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == 100) {
            mBinding.edPhoneNumber.setText(data?.getStringExtra("phone").toString())
            mBinding.edPwd.setText(data?.getStringExtra("pwd").toString())
        }
        if (requestCode == 1000 && resultCode == 1000) {
            finish()
        }
    }
}